<?php

namespace app\admin\controller;

use app\admin\common\Base;
use app\admin\model\Rule as RuleModel;
/**
 * Class Rule 权限控制器
 * @package app\admin\controller
 */
class Rule extends Base {
    public function index(){
        if (request()->isPost()){
            $list = db('Rule')->order('sort','asc')->select();
            //open为true，列表中有子级，默认展开，若无需展开，将该段代码注释
            foreach ($list as $k => $v){
                $list[$k]['open'] = true;
            }
            return json(['code' => 0,'data'=>$list]);
        }
        return $this->fetch();
    }

    public function add(){
        if (request()->isPost()){
            $data = input('post.');
            $model = new RuleModel();
            $result = $model->saveData($data,'add');
            return json(['code' => $result['code'], 'msg' => $result['msg']]);

        }
        $node_list = db('Rule')
            ->where('status',1)
            ->order('sort','asc')
            ->select();
        $node_list = $this->getTree($node_list);
        $this->assign('node_list',$node_list);
        return $this->fetch();
    }

    public function addSon(){
        $pid = input('pid');
        $parent = db('rule')
            ->where('id',$pid)
            ->field('id,name')
            ->find();
        $this->assign('parent',$parent);
        return $this->fetch();
    }

    public function edit($id){

        if (request()->isPost()){
            $data = input('post.');
            $model = new RuleModel();
            $result = $model->saveData($data,'edit',$id);

            return json(['code' => $result['code'], 'msg' => $result['msg']]);

        }
        $info = db('rule')
            ->where('id',$id)
            ->find();
        $this->assign('info',$info);
        $node_list = db('rule')
            ->where('status',1)
            ->order('sort','asc')
            ->select();
        $node_list = $this->getTree($node_list);
        $this->assign('node_list',$node_list);
        return $this->fetch();
    }

    public function del($id){
        if (request()->isPost()){
            $model = new RuleModel();
            $result = $model->delData($id);
            return json(['code' => $result['code'], 'msg' => $result['msg']]);
        }
        $this->error('非法操作！');
    }

    public function changeStatus($id,$action){
        $data['status'] = 1;
        $msg = '已启用';
        $log_msg = '启用权限：';

        if ($action == 'off'){
            $data['status'] = 0;
            $msg = '已停用';
            $log_msg = '停用权限：';
        }

        $result = db('rule')
            ->where('id',$id)
            ->update($data);
        if ($result){
            insert_system_log($log_msg . $id);
            return json(['code' => 'suc', 'msg' => $msg]);
        }
        return json(['code' => 'err', 'msg' => $msg]);
    }

    public function treeList(){
        $where[] = ['status','=',1];
        if (get_user_id() !== 1){
            //查询当前账号拥有的角色
            $user_role = db('user_role')->where('user_id',get_user_id())->column('role_id');
            //角色拥有的权限
            $role_rule = db('role_rule')->where('role_id','in',$user_role)->column('rule_id');
            $where[] = ['id','in',$role_rule];
        }
        $list = db('rule')
            ->where($where)
            ->field('id,name,pid')
            ->select();
        $ids = [];
        foreach ($list as $v){
            $ids[] = intval($v['id']);
        }
        $list = list_to_tree($list);
        return json(['code'=>'suc','data'=>$list,'ids'=>$ids]);
    }
}